# _*_ coding:utf-8 _*_
"""
@author:lx
@file: THR
@contact: lixiang-929@outlook.com
@time: 2019/03/24
"""
import cv2
import numpy as np
import time
import os

# data_path = ".//yellow//"
data_path = ".//red//"
result_path = ".//result//"

def THR(orgin_image):
    start_time = time.time()
    image_name = os.path.join(data_path, orgin_image)
    target_image = cv2.imread(image_name, 0)
    imin, imax = cv2.minMaxLoc(target_image)[:2]
    thr = int((imin + imax) / 2)
    ret, target_image_THR = cv2.threshold(target_image, 15, 255, cv2.THRESH_BINARY)
    file_name, file_format = os.path.splitext(orgin_image)
    red_THR = os.path.join(result_path, file_name + "_THR.jpg")
    # yellow_THR = os.path.join(result_path, file_name + "_THR.jpg")
    cv2.imwrite(red_THR, target_image_THR)
    # cv2.imwrite(yellow_THR, target_image_THR)
    print("time={0:.3f}, {1:s}".format((time.time() - start_time), file_name))
    cv2.waitKey(10)

if __name__ == "__main__":
    data_list = os.listdir(data_path)
    # print(data_list)
    for data in data_list:
        THR(data)
